﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web;

namespace ArcGisMapDesktop
{
    /// <summary>
    /// ConvertTool 的摘要说明
    /// </summary>
    public class ConvertTool
    {

        #region 获取配置文件字段
        /// <summary>
        /// 获取AppSeting字符串函数
        /// </summary>
        /// <param name="Str">所要获取的字符串</param>
        /// <returns></returns>
        public static string GetAppSeting(string key)
        {

            if (key == null || key == "")
                return "";
            string outstr = "";
            outstr = System.Configuration.ConfigurationManager.AppSettings[key];
            if (outstr == null || outstr == "")
                return "";

            return outstr;
        }
        #endregion

        #region string
        /// <summary>
        /// 字符串如果为空，取默认值
        /// </summary>
        /// <param name="strBase">原始字符串</param>
        /// <param name="strDefault">默认字符串</param>
        /// <returns></returns>
        public static string GetDefaultString(string strBase, string strDefault)
        {
            //如果传入的字符串为空 则返回空串
            if (strBase == null || strBase == string.Empty || strBase == "&nbsp;")
            {
                return strDefault;
            }
            return strBase;
        }
        /// <summary>
        /// 返回银行简称
        /// </summary>
        /// <param name="code">银行编码</param>
        /// <returns></returns>
        public static string GetPayMethod(string code)
        {
            string Bank = string.Empty;
            switch (code)
            {
                case "0102":
                    Bank = "ICBC";
                    break;
                case "0105":
                    Bank = "CCB";
                    break;
                case "0103":
                    Bank = "ABC";
                    break;
                case "0":
                    Bank = "CMB";
                    break;
                case "0104":
                    Bank = "BC";
                    break;
                case "0100":
                    Bank = "PSBC";
                    break;
                case "0410":
                    Bank = "PingAn";
                    break;
                case "0302":
                    Bank = "ZhongXin";
                    break;
                case "0303":
                    Bank = "CEB";
                    break;
                case "0423":
                    Bank = "HangZhou";
                    break;
                case "0305":
                    Bank = "CMBC";
                    break;
                case "0304":
                    Bank = "HuaXia";
                    break;
                case "0403":
                    Bank = "BCCB";
                    break;
                case "0310":
                    Bank = "PuFa";
                    break;
                case "0306":
                    Bank = "GuangFa";
                    break;
                case "1418":
                    Bank = "BJNongShang";
                    break;
                case "2502":
                    Bank = "DongYa";
                    break;
                case "0424":
                    Bank = "NanJing";
                    break;
                case "0408":
                    Bank = "NingBo";
                    break;
            }
            return Bank;
        }

        /// <summary>
        /// 读取CSV文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="skip">跳过行</param>
        /// <returns>返回数据</returns>
        public static DataTable ReadCSV(string path, int skip = 1)
        {
            var lines = System.IO.File.ReadAllLines(path).Skip(skip).ToList();

            var dt = new DataTable();
            var cols = lines[0].Split('\t');

            foreach (var col in cols)
            {
                dt.Columns.Add(col);
            }
            var rows = lines.Skip(2).ToList();
            foreach (var row in rows)
            {
                var dr = dt.NewRow();
                var dataCols = row.Split('\t');
                for (int col = 0; col < dataCols.Length; col++)
                {
                    dr[col] = dataCols[col];
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        ///// <summary>
        ///// 导出CSV文件
        ///// </summary>
        ///// <param name="path"></param>
        ///// <param name="dt"></param>
        //public static void WriteCSV(string path, DataTable dt)
        //{
        //    var title = dt.Columns.Cast<DataColumn>()
        //                .Select(m => m.ColumnName)
        //                .ToList();
        //    var rows = dt.Rows.Cast<DataRow>()
        //                .Select(m => string.Join("\t", m.ItemArray))
        //                .ToList();
        //    rows.Insert(0, string.Join("\t", title));

        //    System.IO.File.WriteAllLines(path, rows, System.Text.Encoding.Unicode);
        //}



        public static string GetString(object obj)
        {
            if ((obj == null) || (obj == DBNull.Value))
            {
                return string.Empty;
            }
            return obj.ToString().Trim();
        }

        public static string GetReplaceString(object obj)
        {
            if ((obj == null) || (obj == DBNull.Value))
            {
                return string.Empty;
            }
            return obj.ToString().Trim().Replace("\"", "");
        }
        #endregion

        #region Boolean
        /// <summary>
        /// 将对象转换为 Boolean 类型
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Boolean 类型结果</returns>
        public static bool TypeToBoolean(object objValue, bool defValue)
        {
            if (objValue != null) return TypeToBoolean(objValue, defValue);
            return defValue;
        }
        /// <summary>
        /// 将对象转换为 Boolean 类型
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Boolean 类型结果</returns>
        public static bool TypeToBoolean(string strValue, bool defValue)
        {
            if (strValue != null)
            {
                if (string.Compare(strValue.ToLower(), "true", true) == 0) return true;
                if (string.Compare(strValue.ToLower(), "false", true) == 0) return false;
            }
            return defValue;
        }
        #endregion Boolean

        #region TinyInt
        /// <summary>
        /// 将对象转换为 TinyInt 类型
        /// 默认返回 0
        /// 范围：0 — 255
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <returns>转换后的 TinyInt 类型结果</returns>
        public static byte TypeToTinyInt(object objValue)
        {
            return TypeToTinyInt(objValue, 0);
        }
        /// <summary>
        /// 将对象转换为 TinyInt 类型
        /// 范围：0 — 255
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 TinyInt 类型结果</returns>
        public static byte TypeToTinyInt(object objValue, byte defValue)
        {
            if (objValue != null) return TypeToTinyInt(objValue.ToString(), defValue);
            return defValue;
        }
        /// <summary>
        /// 将对象转换为 TinyInt 类型
        /// 默认返回 0
        /// 范围：0 — 255
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <returns>转换后的 TinyInt 类型结果</returns>
        public static byte TypeToTinyInt(string strValue)
        {
            return TypeToTinyInt(strValue, 0);
        }
        #endregion TinyInt

        #region Int16
        /// <summary>
        /// 将对象转换为 Int16 类型
        /// 默认返回 0
        /// 范围：-32,768 — 32,767
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <returns>转换后的 Int16 类型结果</returns>
        public static short TypeToInt16(object objValue)
        {
            return TypeToInt16(objValue, 0);
        }
        /// <summary>
        /// 将对象转换为 Int16 类型
        /// 范围：-32,768 — 32,767
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Int16 类型结果</returns>
        public static short TypeToInt16(object objValue, short defValue)
        {
            if (objValue != null) return TypeToInt16(objValue.ToString(), defValue);
            return defValue;
        }
        /// <summary>
        /// 将对象转换为 Int16 类型
        /// 默认返回 0
        /// 范围：-32,768 — 32,767
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <returns>转换后的 Int16 类型结果</returns>
        public static short TypeToInt16(string strValue)
        {
            return TypeToInt16(strValue, 0);
        }
        /// <summary>
        /// 将对象转换为 Int16 类型
        /// 范围：-32,768 — 32,767
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Int16 类型结果</returns>
        public static short TypeToInt16(string strValue, short defValue)
        {
            if (string.IsNullOrEmpty(strValue) || strValue.Trim().Length > 6 || !Regex.IsMatch(strValue.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$")) return defValue;
            short rv;
            if (Int16.TryParse(strValue, out rv)) return rv;
            //return Convert.ToInt16(TypeToFloat(strValue, defValue));
            try
            {
                return Convert.ToInt16(strValue);
            }
            catch
            {
                return defValue;
            }
        }
        #endregion Int16

        #region GUID
        /// <summary>
        /// 将对象转换成GUID
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static Guid TypeToGuid(string str)
        {
            try
            {
                Guid g = new Guid(str);
                return g;
            }
            catch
            {
                return Guid.Empty;
            }
        }

        //public static bool IsGuidByParse(string strSrc)
        //{
        //    Guid g = Guid.Empty;
        //    return Guid.TryParse(strSrc, out g);
        //}
        #endregion

        #region Int32
        /// <summary>
        /// 将对象转换为 Int32 类型
        /// 默认返回 0
        /// 范围：-2,147,483,648 — 2,147,483,647
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <returns>转换后的 Int32 类型结果</returns>
        public static int TypeToInt32(object objValue)
        {
            return TypeToInt32(objValue, 0);
        }
        /// <summary>
        /// 将对象转换为 Int32 类型
        /// 范围：-2,147,483,648 — 2,147,483,647
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Int32 类型结果</returns>
        public static int TypeToInt32(object objValue, int defValue)
        {
            if (objValue != null) return TypeToInt32(objValue.ToString(), defValue);
            return defValue;
        }


        /// <summary>
        /// 将对象转换为 Int32 类型
        /// 默认返回 0
        /// 范围：-2,147,483,648 — 2,147,483,647
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <returns>转换后的 Int32 类型结果</returns>
        public static int TypeToInt32(string strValue)
        {
            return TypeToInt32(strValue, 0);
        }
        /// <summary>
        /// 将对象转换为 Int32 类型
        /// 范围：-2,147,483,648 — 2,147,483,647
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Int32 类型结果</returns>
        public static int TypeToInt32(string strValue, int defValue)
        {
            if (string.IsNullOrEmpty(strValue) || strValue.Trim().Length > 11 || !Regex.IsMatch(strValue.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$")) return defValue;
            int rv;
            if (Int32.TryParse(strValue, out rv)) return rv;
            return Convert.ToInt32(TypeToFloat(strValue, defValue));
        }
        #endregion Int32

        #region Int64
        /// <summary>
        /// 将对象转换为 Int64 类型
        /// 默认返回 0
        /// 范围：-9,223,372,036,854,775,808 — 9,223,372,036,854,775,807
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <returns>转换后的 Int64 类型结果</returns>
        public static long TypeToInt64(object objValue)
        {
            return TypeToInt64(objValue, 0);
        }
        /// <summary>
        /// 将对象转换为 Int64 类型
        /// 范围：-9,223,372,036,854,775,808 — 9,223,372,036,854,775,807
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Int64 类型结果</returns>
        public static long TypeToInt64(object objValue, long defValue)
        {
            if (objValue != null) return TypeToInt64(objValue.ToString(), defValue);
            return defValue;
        }
        /// <summary>
        /// 将对象转换为 Int64 类型
        /// 默认返回 0
        /// 范围：-9,223,372,036,854,775,808 — 9,223,372,036,854,775,807
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <returns>转换后的 Int64 类型结果</returns>
        public static long TypeToInt64(string strValue)
        {
            return TypeToInt64(strValue, 0);
        }
        /// <summary>
        /// 将对象转换为 Int64 类型
        /// 范围：-9,223,372,036,854,775,808 — 9,223,372,036,854,775,807
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Int64 类型结果</returns>
        public static long TypeToInt64(string strValue, long defValue)
        {
            if (string.IsNullOrEmpty(strValue) || strValue.Trim().Length > 20 || !Regex.IsMatch(strValue.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$")) return defValue;
            long rv;
            if (Int64.TryParse(strValue, out rv)) return rv;
            //return Convert.ToInt64(TypeToFloat(strValue, defValue));
            try
            {
                return Convert.ToInt64(strValue);
            }
            catch
            {
                return defValue;
            }
        }
        #endregion Int64

        #region Float
        /// <summary>
        /// 将对象转换为 Float 类型
        /// 范围：- 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Float 类型结果</returns>
        public static float TypeToFloat(object objValue, float defValue)
        {
            if ((objValue == null)) return defValue;
            return TypeToFloat(objValue.ToString(), defValue);
        }
        /// <summary>
        /// 将对象转换为 Float 类型
        /// 默认返回 0
        /// 范围：- 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <returns>转换后的 Float 类型结果</returns>
        public static float TypeToFloat(object objValue)
        {
            return TypeToFloat(objValue.ToString(), 0);
        }
        /// <summary>
        /// 将对象转换为 Float 类型
        /// 默认返回 0
        /// 范围：- 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <returns>转换后的 Float 类型结果</returns>
        public static float TypeToFloat(string strValue)
        {
            if ((strValue == null)) return 0;
            return TypeToFloat(strValue, 0);
        }
        /// <summary>
        /// 将对象转换为 Float 类型
        /// 范围：- 1.79E+308 to -2.23E-308, 0 and 2.23E-308 to 1.79E+308
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Float 类型结果</returns>
        public static float TypeToFloat(string strValue, float defValue)
        {
            if ((strValue == null) || (strValue.Length > 10)) return defValue;
            float intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat) float.TryParse(strValue, out intValue);
            }
            return intValue;
        }
        #endregion Float

        #region Decimal
        /// <summary>
        /// 将对象转换为 Decimal 类型
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Decimal 类型结果</returns>
        public static decimal TypeToDecimal(object objValue, decimal defValue)
        {
            if ((objValue == null)) return defValue;
            return TypeToDecimal(objValue.ToString(), defValue);
        }
        /// <summary>
        /// 将对象转换为 Decimal 类型
        /// <remarks>
        /// 默认返回 0
        /// </remarks>
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <returns>转换后的 Decimal 类型结果</returns>
        public static decimal TypeToDecimal(object objValue)
        {
            return TypeToDecimal(objValue.ToString(), 0);
        }
        /// <summary>
        /// 将对象转换为 Decimal 类型
        /// <remarks>
        /// 默认返回 0
        /// </remarks>
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <returns>转换后的 Decimal 类型结果</returns>
        public static decimal TypeToDecimal(string strValue)
        {
            if ((strValue == null)) return 0;
            return TypeToDecimal(strValue, 0);
        }
        /// <summary>
        /// 将对象转换为 Decimal 类型
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Decimal 类型结果</returns>
        public static decimal TypeToDecimal(string strValue, decimal defValue)
        {
            if ((strValue == null) || (strValue.Length > 10)) return defValue;
            decimal intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat) decimal.TryParse(strValue, out intValue);
            }
            return intValue;
        }
        /// <summary>
        /// 转百分比
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string TypeToPercent(object objValue)
        {
            decimal val = TypeToDecimal(objValue.ToString(), 0) * 100;
            return string.Format("{0}%", val);
        }

        /// <summary>
        /// 替换
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ValueToReplace(object objValue, string first, string last)
        {
            string val = GetString(objValue);
            return val.Replace(first, last);
        }

        #endregion decimal

        #region double
        /// <summary>
        /// 将对象转换为 Decimal 类型
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Decimal 类型结果</returns>
        public static double TypeToDouble(object objValue, double defValue)
        {
            if ((objValue == null)) return defValue;
            return TypeToDouble(objValue.ToString(), defValue);
        }
        /// <summary>
        /// 将对象转换为 Decimal 类型
        /// <remarks>
        /// 默认返回 0
        /// </remarks>
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <returns>转换后的 Decimal 类型结果</returns>
        public static double TypeToDouble(object objValue)
        {
            return TypeToDouble(objValue.ToString(), 0);
        }
        /// <summary>
        /// 将对象转换为 Decimal 类型
        /// <remarks>
        /// 默认返回 0
        /// </remarks>
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <returns>转换后的 Decimal 类型结果</returns>
        public static double TypeToDouble(string strValue)
        {
            if ((strValue == null)) return 0;
            return TypeToDouble(strValue, 0);
        }
        /// <summary>
        /// 将对象转换为 Decimal 类型
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 Decimal 类型结果</returns>
        public static double TypeToDouble(string strValue, double defValue)
        {
            if ((strValue == null) || (strValue.Length > 10)) return defValue;
            double intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat) double.TryParse(strValue, out intValue);
            }
            return intValue;
        }
        #endregion decimal

        #region DateTime
        /// <summary>
        /// 将对象转换为 DateTime 类型
        /// </summary>
        /// <param name="year">年份</param>
        /// <returns></returns>
        public static DateTime StrToDateTime(int year)
        {
            return new DateTime(year, 1, 1);
        }
        /// <summary>
        /// 将对象转换为 DateTime 类型
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <returns></returns>
        public static DateTime StrToDateTime(int year, int month)
        {
            return new DateTime(year, month, 1);
        }
        /// <summary>
        /// 将对象转换为 DateTime 类型
        /// </summary>
        /// <param name="year">年份</param>
        /// <param name="month">月份</param>
        /// <param name="day">日期</param>
        /// <returns></returns>
        public static DateTime StrToDateTime(int year, int month, int day)
        {
            return new DateTime(year, month, day);
        }
        /// <summary>
        /// 将对象转换为 DateTime 类型
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 DateTime 类型结果</returns>
        public static DateTime StrToDateTime(string strValue, DateTime defValue)
        {
            if (!string.IsNullOrEmpty(strValue))
            {
                DateTime dateTime;
                if (DateTime.TryParse(strValue, out dateTime)) return dateTime;
            }
            return defValue;
        }
        /// <summary>
        /// 将对象转换为 DateTime 类型
        /// 默认返回 当前时间
        /// </summary>
        /// <param name="strValue">要转换的值</param>
        /// <returns>转换后的 DateTime 类型结果</returns>
        public static DateTime StrToDateTime(string strValue)
        {
            return StrToDateTime(strValue, DateTime.Now);
        }
        /// <summary>
        /// 将对象转换为 DateTime 类型
        /// 默认返回 当前时间
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <returns>转换后的 DateTime 类型结果</returns>
        public static DateTime ObjectToDateTime(object objValue)
        {
            return StrToDateTime(objValue.ToString());
        }
        /// <summary>
        /// 将对象转换为 DateTime 类型
        /// </summary>
        /// <param name="objValue">要转换的值</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的 DateTime 类型结果</returns>
        public static DateTime ObjectToDateTime(object objValue, DateTime defValue)
        {
            return StrToDateTime(objValue.ToString(), defValue);
        }

        /// <summary>
        /// 获得yyyy-MM-dd格式的日期串
        /// </summary>
        /// <param name="strDatetime"></param>
        /// <returns></returns>
        public static DateTime? GetFormatDateByString(object strDatetime)
        {
            if (strDatetime == null)
            {
                return null;
            }
            if (strDatetime.ToString() == string.Empty)
            {
                return null;
            }
            try
            {
                return (DateTime?)Convert.ToDateTime(strDatetime);
            }
            catch
            {
                return null;
            }
        }
        #endregion DateTime

        #region Color
        /// <summary>
        /// 将字符串转换为Color
        /// </summary>
        /// <param name="strColor">颜色字符串</param>
        /// <returns></returns>
        public static Color ToColor(string strColor)
        {
            int red, green, blue = 0;
            char[] rgb;
            strColor = strColor.TrimStart('#');
            strColor = Regex.Replace(strColor.ToLower(), "[g-zG-Z]", "");
            switch (strColor.Length)
            {
                case 3:
                    rgb = strColor.ToCharArray();
                    red = Convert.ToInt32(rgb[0].ToString() + rgb[0].ToString(), 16);
                    green = Convert.ToInt32(rgb[1].ToString() + rgb[1].ToString(), 16);
                    blue = Convert.ToInt32(rgb[2].ToString() + rgb[2].ToString(), 16);
                    return Color.FromArgb(red, green, blue);
                case 6:
                    rgb = strColor.ToCharArray();
                    red = Convert.ToInt32(rgb[0].ToString() + rgb[1].ToString(), 16);
                    green = Convert.ToInt32(rgb[2].ToString() + rgb[3].ToString(), 16);
                    blue = Convert.ToInt32(rgb[4].ToString() + rgb[5].ToString(), 16);
                    return Color.FromArgb(red, green, blue);
                default:
                    return Color.FromName(strColor);
            }
        }
        #endregion Color

        #region Base64
        /// <summary>
        /// 将普通字符串转换为Base64字符串
        /// </summary>
        /// <param name="theStr">待转换字符串</param>
        /// <returns>Base64字符串</returns>
        public static string ToBase64Encode(string theStr)
        {
            return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(theStr)).Replace("+", "%2B");
        }
        /// <summary>
        /// 将Base64字符串转换为普通字符串
        /// </summary>
        /// <param name="theStr">待转换字符串</param>
        /// <returns>普通字符串</returns>
        public static string ToBase64Decode(string theStr)
        {
            return System.Text.Encoding.Default.GetString(Convert.FromBase64String(theStr.Replace("%2B", "+")));
        }
        #endregion Base64

        #region GetDistance
        //根据两点经纬度计算距离
        private static double rad(double d)
        {
            return d * Math.PI / 180.0;
        }
        /// <summary>
        /// 计算两点间的距离
        /// </summary>
        /// <param name="lat1"></param>
        /// <param name="lng1"></param>
        /// <param name="lat2"></param>
        /// <param name="lng2"></param>
        /// <returns></returns>
        public static double GetDistance(double lat1, double lng1, double lat2, double lng2)
        {
            double EARTH_RADIUS = 6378.137;//地球半径
            double radLat1 = rad(lat1);
            double radLat2 = rad(lat2);
            double a = radLat1 - radLat2;
            double b = rad(lng1) - rad(lng2);
            double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2)));
            result = Math.Round(result * EARTH_RADIUS * 10000) / 10;
            return result;
        }
        ////上面的方法等价的SQL语句，如坐标22.551743,113.937363
        //select Name,(
        //ACOS(
        //SIN((22.551743 * 3.1415) / 180 ) * SIN((latitude * 3.1415) / 180 ) +
        //COS((22.551743 * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *
        //COS((113.937363 * 3.1415) / 180 - (longitude * 3.1415) / 180 )
        //) * 6378.137
        //) as distance
        //from Shop 
        //where ID!=1 and Latitude != 22.551819 
        //order by distance asc
        #endregion GetDistance

        #region Cache
        /// <summary>  
        /// 获取当前应用程序指定CacheKey的Cache值  
        /// </summary>  
        /// <param name="CacheKey"></param>  
        /// <returns></returns>  
        public static object GetCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            return objCache[CacheKey];
        }

        /// <summary>  
        /// 查询指定CacheKey的Cache值是否存在  
        /// </summary>  
        /// <param name="CacheKey"></param>  
        /// <returns></returns>  
        public static bool IsExistsCache(string CacheKey)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            if (objCache[CacheKey] == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>  
        /// 设置当前应用程序指定CacheKey的Cache值  
        /// </summary>  
        /// <param name="CacheKey"></param>  
        /// <param name="objObject"></param>  
        public static void SetCache(string CacheKey, object objObject)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject);
        }

        /// <summary>  
        /// 设置当前应用程序指定CacheKey的Cache值  
        /// </summary>  
        /// <param name="CacheKey"></param>  
        /// <param name="objObject"></param>  
        public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }

        /// <summary>  
        /// 设置数据缓存  
        /// </summary>  
        public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
        }

        /// <summary>  
        /// 移除指定数据缓存  
        /// </summary>  
        public static void RemoveAllCache(string CacheKey)
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            _cache.Remove(CacheKey);
        }
        /// <summary>  
        /// 移除全部缓存  
        /// </summary>  
        public static void RemoveAllCache()
        {
            System.Web.Caching.Cache _cache = HttpRuntime.Cache;
            IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
            while (CacheEnum.MoveNext())
            {
                _cache.Remove(CacheEnum.Key.ToString());
            }
        }

        /// <summary>
        /// 唯一订单号生成
        /// </summary>
        /// <returns></returns>
        public static string GenerateOrderNumber()
        {
            Thread.Sleep(300);
            string strDateTimeNumber = DateTime.Now.ToString("yyyyMMddHHmmssms");
            string strRandomResult = NextRandom(1000, 1).ToString();
            return strDateTimeNumber + strRandomResult;
        }
        /// <summary>
        /// 参考：msdn上的RNGCryptoServiceProvider例子
        /// </summary>
        /// <param name="numSeeds"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private static int NextRandom(int numSeeds, int length)
        {
            // Create a byte array to hold the random value.  
            byte[] randomNumber = new byte[length];
            // Create a new instance of the RNGCryptoServiceProvider.  
            System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
            // Fill the array with a random value.  
            rng.GetBytes(randomNumber);
            // Convert the byte to an uint value to make the modulus operation easier.  
            uint randomResult = 0x0;
            for (int i = 0; i < length; i++)
            {
                randomResult |= ((uint)randomNumber[i] << ((length - 1 - i) * 8));
            }
            return (int)(randomResult % numSeeds) + 1;
        }

        #endregion cache

        #region 用星号代替
        public static string PlusXing(string str, int frontLen, int endLen)
        {
            var len = str.Length - frontLen - endLen;
            var xing = "";
            for (var i = 0; i < len; i++)
            {
                xing += "*";
            }
            return str.Substring(0, frontLen) + xing + str.Substring(str.Length - endLen);
        }
        #endregion

        #region Base64加密解密
        /// <summary>
        /// 密码加密
        /// </summary>
        public static string Base64Encode(string pwd)
        {
            return Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(pwd));
        }

        /// <summary>
        /// 密码解密
        /// </summary>
        public static string Base64Decode(string pwd)
        {
            return System.Text.Encoding.Default.GetString(Convert.FromBase64String(pwd));
        }
        #endregion Base64加密解密

        #region 导入EXCEL
        /// <summary>
        /// 导入EXCEL到DataSet(第一行将会作为ColumnName)
        /// </summary>
        public static DataSet ExcelDs(string fileUrl)
        {
            var lastname = System.IO.Path.GetExtension(fileUrl);
            string strConn = "";
            if (lastname == ".xls")
            {
                strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + fileUrl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";
            }
            if (lastname == ".xlsx")
            {
                strConn = "Provider=Microsoft.ACE.OleDb.12.0;" + "data source=" + fileUrl + ";Extended Properties='Excel 12.0; HDR=YES; IMEX=1'";
            }
            DataSet ds = new DataSet();
            using (OleDbConnection conn1 = new OleDbConnection(strConn))
            {
                conn1.Open();
                var _Table = conn1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                foreach (DataRow dt in _Table.Rows)
                {
                    string tbname = dt[2].ToString();
                    OleDbDataAdapter odda = new OleDbDataAdapter("select * from [" + tbname + "]", conn1);
                    odda.Fill(ds, tbname);
                }
                conn1.Close();
                conn1.Dispose();
            }
            return ds;
        }
        #endregion 导入EXCEL

        #region json转化
        /// <summary>
        /// 把json字符串反序列化成对象
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static object deserializeJsonObject(string jsonString)
        {
            return JsonConvert.DeserializeObject(jsonString);
        }

        /// <summary>
        /// 把json对象序列化成字符串
        /// </summary>
        /// <param name="jsonObj"></param>
        /// <returns></returns>
        public static string serializeJsonString(object jsonObject)
        {
            return JsonConvert.SerializeObject(jsonObject);
        }
        #endregion

        #region 获取浏览器首选语言
        /// <summary>
        /// 获取浏览器首选语言
        /// </summary>
        /// <returns></returns>
        public static string GetBrowserLang()
        {
            string lang = HttpContext.Current.Request.UserLanguages[0];

            if (lang.Contains("zh"))
                return "CN";

            if (lang.Contains("en"))
                return "EN";

            if (lang.Contains("de"))
                return "DE";

            if (lang.Contains("fr"))
                return "FR";

            if (lang.Contains("es"))
                return "ES";

            if (lang.Contains("it"))
                return "IT";

            return "EN";
        }
        #endregion

        #region 获取配置文件字段
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="key">关键词</param>
        /// <param name="value">值</param>
        /// <param name="expires">过期时间</param>
        public static void SetCookie(string key, string value, DateTime expires)
        {
            HttpCookie cookie = new HttpCookie(key);
            cookie.Value = value;
            cookie.Expires = expires;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 读取cookie
        /// </summary>
        /// <param name="key">关键词</param>
        /// <returns></returns>
        public static string GetCookie(string key)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie != null)
            {
                return cookie.Value;
            }
            return null;
        }

        /// <summary>
        /// 设置cookie过期
        /// </summary>
        /// <param name="key">关键词</param>
        /// <returns></returns>
        public static void SetCookieExpired(string key)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            if (cookie != null)
            {
                cookie.Expires = DateTime.Now.AddDays(-2);
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
        }
        #endregion
    }
}